<?php
require_once "header.php";
require_once "include/rss_fetch.inc";
require_once "include/functions.php";

if (empty($xoopsUser)) {
    $cookie_city_id = isset($_COOKIE['xoops_junkweather']) ? intval($_COOKIE['xoops_junkweather']) : 0;
    if ($cookie_city_id != 0) {
        $u_city_id = $cookie_city_id;
    } else {
        $u_city_id = $xoopsModuleConfig["default_city"];
    } 
} else {
    $result = $xoopsDB->query("SELECT * FROM " . $xoopsDB->prefix("junkweather") . ' WHERE user_id = ' . $xoopsUser->getVar('uid'));
    $myrow = $xoopsDB->fetchRow($result);
    if ($myrow) {
        $u_city_id = $myrow[1];
    } else {
        $u_city_id = $xoopsModuleConfig["default_city"];
    } 
} 

$city_id = isset($_GET['city_id']) ? intval($_GET['city_id']) : $u_city_id;

if (empty($city[$city_id])) {
    redirect_header(XOOPS_URL , 3, _MD_JWEATHER_NONECITY);
    exit();
} 

$url = "http://weather.goo.ne.jp/area/" . $city_id . ".rdf";
$rss = fetch_rss($url);
if ($rss == false) {
    redirect_header(XOOPS_URL , 3, _MD_JWEATHER_ERROR);
    exit();
} 
$item = $rss->items;

include(XOOPS_ROOT_PATH . '/header.php');

echo "<h2>" . _MD_JWEATHER_NAME . "</h2>";
echo $city[$city_id] . _MD_JWEATHER_NOWVIEW . "<br />";
$encoded = my_mb_convert($item[0]['content']['encoded']);
// $encoded = str_replace("<a","<a target=\"_blank\"",$encoded);
echo $encoded;

if ($xoopsModuleConfig["view_mode"] == 1) {
    $submit = _MD_JWEATHER_SUBMIT;

    print <<<BODY
<form action="config.php" method="post">
<input type="hidden" name="city_id" value="{$city_id}">
<input type="submit" value="{$submit}">
</form>
BODY;

    $change_city = _MD_JWEATHER_CHCITY;

    $cols = $xoopsModuleConfig["view_cols"];
    $colp = 100 / $cols;

    echo '<table class="outer" width="100%" cellspacing="1" style="margin-bottom:10px;">';
    echo '<tr><th colspan=' . $cols . '>' . $change_city . '</th></tr>';
    $m_count = 0;
    $bkey = "";
    foreach($city as $id => $name) {
        if (!$bkey) {
            if ($m_count > 0) {
                echo '<td class="even" colspan="' . ($cols - $m_count) . '">&nbsp;</td></tr>';
            } 
            echo '<tr><th colspan=' . $cols . '>' . _MD_JWEATHER_CATID1 . '</th></tr>';
            $m_count = 0;
        } elseif ($id > 3000 && $bkey < 3000) {
            if ($m_count > 0) {
                echo '<td class="even" colspan="' . ($cols - $m_count) . '">&nbsp;</td></tr>';
            } 
            echo '<tr><th colspan=' . $cols . '>' . _MD_JWEATHER_CATID2 . '</th></tr>';
            $m_count = 0;
        } elseif ($id > 4000 && $bkey < 4000) {
            if ($m_count > 0) {
                echo '<td class="even" colspan="' . ($cols - $m_count) . '">&nbsp;</td></tr>';
            } 
            echo '<tr><th colspan=' . $cols . '>' . _MD_JWEATHER_CATID3 . '</th></tr>';
            $m_count = 0;
        } elseif ($id > 5000 && $bkey < 5000) {
            if ($m_count > 0) {
                echo '<td class="even" colspan="' . ($cols - $m_count) . '">&nbsp;</td></tr>';
            } 
            echo '<tr><th colspan=' . $cols . '>' . _MD_JWEATHER_CATID4 . '</th></tr>';
            $m_count = 0;
        } elseif ($id > 6000 && $bkey < 6000) {
            if ($m_count > 0) {
                echo '<td class="even" colspan="' . ($cols - $m_count) . '">&nbsp;</td></tr>';
            } 
            echo '<tr><th colspan=' . $cols . '>' . _MD_JWEATHER_CATID5 . '</th></tr>';
            $m_count = 0;
        } elseif ($id > 6600 && $bkey < 6600) {
            if ($m_count > 0) {
                echo '<td class="even" colspan="' . ($cols - $m_count) . '">&nbsp;</td></tr>';
            } 
            echo '<tr><th colspan=' . $cols . '>' . _MD_JWEATHER_CATID6 . '</th></tr>';
            $m_count = 0;
        } elseif ($id > 8200 && $bkey < 8200) {
            if ($m_count > 0) {
                echo '<td class="even" colspan="' . ($cols - $m_count) . '">&nbsp;</td></tr>';
            } 
            echo '<tr><th colspan=' . $cols . '>' . _MD_JWEATHER_CATID7 . '</th></tr>';
            $m_count = 0;
        } 

        if ($m_count == 0) {
            echo "<tr>";
        } 
        if ($id == $city_id) {
            echo "<td class='head' style='width: " . $colp . "%; text-align: center;'>" . $name . "</td>\n";
        } else if ($id == $u_city_id) {
            echo "<td class='even' style='width: " . $colp . "%; text-align: center;'><font color='red'>*</font> <a href='./index.php?city_id=" . $id . "'>" . $name . "</a></td>\n";
        } else {
            echo "<td class='even' style='width: " . $colp . "%; text-align: center;'><a href='./index.php?city_id=" . $id . "'>" . $name . "</a></td>\n";
        } 
        $m_count++;
        if ($m_count >= $cols) {
            echo "</tr>\n";
            $m_count = 0;
        } 
        $bkey = $id;
    } 
    if ($m_count > 0) {
        echo '<td class="even" colspan="' . ($cols - $m_count) . '">&nbsp;</td></tr>';
    } 
    echo "</table>\n";
} else {
    $bkey = "";
    $form = "";
    foreach($city as $key => $val) {
        if (!$bkey) {
            $form .= '<optgroup label="' . _MD_JWEATHER_CATID1 . '">' . "\n";
        } elseif ($key > 3000 && $bkey < 3000) {
            $form .= '<optgroup label="' . _MD_JWEATHER_CATID2 . '">' . "\n";
        } elseif ($key > 4000 && $bkey < 4000) {
            $form .= '<optgroup label="' . _MD_JWEATHER_CATID3 . '">' . "\n";
        } elseif ($key > 5000 && $bkey < 5000) {
            $form .= '<optgroup label="' . _MD_JWEATHER_CATID4 . '">' . "\n";
        } elseif ($key > 6000 && $bkey < 6000) {
            $form .= '<optgroup label="' . _MD_JWEATHER_CATID5 . '">' . "\n";
        } elseif ($key > 6600 && $bkey < 6600) {
            $form .= '<optgroup label="' . _MD_JWEATHER_CATID6 . '">' . "\n";
        } elseif ($key > 8200 && $bkey < 8200) {
            $form .= '<optgroup label="' . _MD_JWEATHER_CATID7 . '">' . "\n";
        } 
        $selected = "";
        if ($key == $city_id) {
            $selected = " selected=selected";
        } 
        $form .= "<option value=\"{$key}\"{$selected}>{$val}</option>\n";
        $bkey = $key;
    } 

    $change_city = _MD_JWEATHER_CHCITY;
    $submit = _MD_JWEATHER_REGIST;

    print <<<BODY
<hr>
<form action="config.php" method="post">
<b>{$change_city}</b>
<select name="city_id">
$form
</select>
<input type=submit value="{$submit}">
</form>
BODY;
} 
include(XOOPS_ROOT_PATH . '/footer.php');

?>